
package ch.unifr.dokpe.htmlpicker;

import java.net.*;
import java.io.*;
import java.util.*;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.jsoup.Jsoup;

/**
 * This class used Jsoup selector to get elements in the page and retrived the text (including children elements) 
 * with <code>Element.text()</code> or <code>Element.ownText()</code> (if label is given in the extractor.strict property).
 * see <a href="http://jsoup.org/cookbook/extracting-data/selector-syntax">http://jsoup.org/cookbook/extracting-data/selector-syntax</a>
 */


public class JsoParser implements HtpParser {


	Map<String,String>  selectors = new HashMap<String,String>();
	Set <String> owntext = new HashSet<String>();
	//Map<Sting,Integer> labels = new HashMap<String, Integer>();
	public static byte DEBUG;

	private String encoding;

	public void init(Properties p){
		DEBUG=Byte.parseByte(p.getProperty("DEBUG"));
		Enumeration em = p.keys();
  		while(em.hasMoreElements()){
  			String str = (String)em.nextElement();
			  //System.out.println("JsoParser.init: " + str);
			  if (str.length() > 14) {
				  String d = str.substring(0,13);
				  if (d.equalsIgnoreCase("target.string")){
					String lab = str.substring(14);
					if (DEBUG>=4)System.out.println("JsoParser.init: " + lab);	
					selectors.put(lab, p.getProperty(str));
					//labels.set(lab, new Integer(0));
				  }
			  }
			  if(str.equalsIgnoreCase("extractor.strict")){
				String allNames = p.getProperty(str);
				if (allNames != null && allNames.length()>0) {
					String [] clNames =allNames.split(";\\s*");
					for (String n : clNames){
						owntext.add(n);
						if (DEBUG>=4) System.out.println("extractor.strict: *" + n + "*");
					}
			  	}
			  }
		
  		}
		encoding = p.getProperty("file.encoding",  "ISO8859_1");

		if (selectors.size() == 0){
				throw new RuntimeException("JsoParser: can't continue: target.String missing");
		}

	}

	public void parseHTML(URL url, MapProvider mp) {

		org.jsoup.nodes.Document doc=null;
		
		mp.setLabels(selectors.keySet());

	 	try {
		   if (url.getProtocol().equals("file")) {
		        doc = Jsoup.parse(new File(url.getFile()),encoding);
		    } else {
			doc = Jsoup.connect(url.toString()).get();
		   }
		 } catch (IOException e){e.printStackTrace();}
		if (DEBUG>=4) System.out.println(doc.toString());
		for (Map.Entry<String, String> sel : selectors.entrySet()){
			//for each selector retrieves the corresponding elements
			String label = sel.getKey();
			Elements elms = doc.select(sel.getValue());
			if (DEBUG>=3) System.out.println("JsoParser: found "+ elms.size() + " occurences for selector " + sel.getValue());

			//with each element retrieved
			for (Element elm : elms){
				String data = (owntext.contains(label)? elm.ownText() : elm.text());
				if (DEBUG>=1) System.out.println("JsoParser found: " + data);	
					//get a new map if necessary or an existing one
					
					Map<String,String> m = mp.getCurrentMap(label);
					m.put(label, data);
				
				}
		
		}
			

	}//parseHTML


}//class

//@MITLI@
